/**
 * Created by Administrator on 2018/10/17 0017.
 */
var ids=[];
var curr_ss=[];
function getListPage(pageurl,id,keyword,type,curr){
    $("#allChk").prop("checked",false);
    ids.push(id);
    sign.params={
        id:id,
        keyword:keyword,
        curr:curr
    };
    var encode_post = sign.encode_post();
    layui.use(['laypage','form', 'layer'], function(){
        var laypage = layui.laypage
            ,layer = layui.layer
            ,form = layui.form;
        var index = layer.load(2, {shade: [0.1,'#fff'],time: 60*1000});

        $.post(pageurl,encode_post, function(data){
            //解码
            if(typeof data == 'string'){
                data = JSON.parse(data);
            }

            datass = sign.decode_post(data);

            //选择
            func_type(type,datass);

            form.render('checkbox');
            layer.close(index);
            if(datass.data.pages<=1){
                $('#pages').html('');
            }
            else {
                //显示分页
                laypage.render({
                    elem: 'pages', 			// 容器
                    count: datass.data.total,     	// 总页数(后台的)
                    curr: datass.data.curr, 		//当前页(后台获取到的)
                    prev: '&laquo;',
                    next: '&raquo;',
                    jump: function (obj, first) {
                        if (!first) {
                            curr_ss.push(obj.curr);
                            getListPage(pageurl, id,keyword,1, obj.curr);
                        }
                    }
                });
            }
        });
    });
}

function func_type(type,data) {
    switch (type){
        case 1:compose_list(data);break;
        case 2:myCompose_list(data);break;
        case 3:allCompose_list(data);break;
        default:break;
    }
}
function compose_list(data) {
    var html='';
    if (data['data']['list'] === undefined || data['data']['list'].length == 0) {
        html ='<tr><td align="center" colspan="12">暂无相关数据</td></tr>';
        //页面数据替换
        $('#page_list').html(html);
    }
    else {
        list = data['data']['list'];
        for (var i in list) {
            list[i].id = list[i].id ? list[i].id : '';
            list[i].title = list[i].title ? list[i].title : '';
            list[i].status = list[i].status==1 ? '未审核' : '已审核';
            list[i].read = list[i].read ? list[i].read : 0;
            list[i].username = list[i].username ? list[i].username : '';
            list[i].content = list[i].content ? list[i].content : '';
            list[i].createtime = list[i].createtime ? list[i].createtime : '';
            list[i].comment = list[i].comment ? list[i].comment : '暂无评论';

            html += '<tr> <td class="tc"><input name="id[]" value="' + list[i].id + '" type="checkbox"></td> <td>';
            // html += '<input name="ids[]" value="' + list[i].id + '" type="hidden">';
            // html += '<input class="common-input sort-input" name="ord[]" value="0" type="text"></td>';
            // html += '<td title="' + list[i].title + '">';
            html += '<a target="_blank" href="#" onclick="articleInfo(' + list[i].id + ')" title="' + list[i].title + '">' + list[i].title + '</a></td>';
            html += '<td>' + list[i].status + '</td>';
            html += '<td>' + list[i].read + '</td>';
            html += '<td>' + list[i].username + '</td>';
            html += '<td>' + list[i].createtime + '</td>';
            html += '<td>'+list[i].comment+'</td><td class="link-update">';
            if (list[i].status == '未审核'){
                html += '<a class="link-update" href="#" onclick="status_compose(' + list[i].id + ',2)">';
                html += '审核</a>';
            }else {
                html += '<a class="link-update" href="#" onclick="status_compose(' + list[i].id + ',1)">';
                html += '禁用</a>';
            }
            html += '<a class="link-del" href="#" onclick="del_compose(' + list[i].id + ')">删除</a>';
            html += '</td></tr>';
        }
        $('#page_list').html(html);
    }
}

function myCompose_list(data) {

    var html='';
    if (data['data']['list'] === undefined || data['data']['list'].length == 0) {
        html ='<tr><td align="center" colspan="12">暂无相关数据</td></tr>';
        //页面数据替换
        $('#page_list').html(html);
    }
    else {
        list = data['data']['list'];
        console.log(list);
        for (var i in list) {
            list[i].id = list[i].id ? list[i].id : '';
            list[i].title = list[i].title ? list[i].title : '';
            list[i].status = list[i].status==1 ? '未审核' : '已审核';
            list[i].read = list[i].read ? list[i].read : 0;
            list[i].content = list[i].content ? list[i].content : '';
            list[i].createtime = list[i].createtime ? list[i].createtime : '';
            list[i].comment = list[i].comment ? list[i].comment : '暂无评论';

            html += '<tr> <td class="tc"><input name="id[]" id="a" value="' + list[i].id + '" type="checkbox"></td> <td>';
            html += '<a target="_blank" href="#" onclick="articleInfo(' + list[i].id + ')" title="' + list[i].title + '">' + list[i].title + '</a></td>';
            html += '<td>' + list[i].status + '</td>';
            html += '<td>' + list[i].read + '</td>';
            html += '<td>' + list[i].createtime + '</td>';
            html += '<td>'+list[i].comment+'</td><td class="link-update">';
            html += '<a class="link-update" href="#" onclick="rewrite_blog(' + list[i].id + ')">修改</a>' +
                '<a class="link-del" href="#"  onclick="del_compose(' + list[i].id + ')">删除</a>';
            html += '</td></tr>';
        }
        $('#page_list').html(html);
    }
}

function allCompose_list(data) {
    var html='';
    list = data['data']['list'];
    for (var i in list) {
        list[i].id = list[i].id ? list[i].id : '';
        list[i].title = list[i].title ? list[i].title : '';
        list[i].img = list[i].img ? list[i].img : '';
        list[i].read = list[i].read ? list[i].read : 0;
        list[i].username = list[i].username ? list[i].username : '';
        list[i].content = list[i].content ? list[i].content : '';
        list[i].newtime = list[i].newtime ? list[i].newtime : '';
        list[i].comment = list[i].comment ? list[i].comment : '0';
        list[i].like = list[i].like ? list[i].like : '0';

        html += '<div class="col-lg-3 col-md-6 single-fashion">'+
                '<img class="img-fluid" style="width: 255px;height: 175px" src="/'+list[i].img +'" alt="">'+
                '<p class="date">'+list[i].newtime+'</p>'+
            '<h4><a href="#" onclick="articleInfo(' + list[i].id + ')" >'+list[i].title+'</a></h4>'+
            '<p>'+list[i].content+
            '</p>'+
            '<div class="meta-bottom d-flex justify-content-between">'+
                '<p><span class="lnr lnr-heart"></span>'+list[i].like+' Likes</p>'+
            '<p><span class="lnr lnr-bubble"></span> '+list[i].comment+' Comments</p>'+
            '</div>'+
            '</div>';
        }
        $('#all_compose_list').html(html);

}

//删除
function del_compose(id) {
    var id = id;
    sign.params={
        id:id,
        del:0
    };
    var encode_post = sign.encode_post();
    $.post(url_list.rewriting,encode_post, function(data){
        getListPage(url_list.myCompose_list,ids[0],keyword,1,curr_ss[0]);
    });
}
//批量删除
function dels_compose(type) {
    var checkedNum = $("input[name='id[]']:checked").length;
    if (checkedNum == 0) {
        layer.msg('请选择至少一项', {icon: 2});
        return;
    }
    // 批量选择
    if (confirm("确定要删除？")) {
        var checkedList = new Array();

        $("input[name='id[]']:checked").each(function () {
            checkedList.push($(this).val());
        });
    }
    var check_id =checkedList.join(',');
    sign.params={
        id :check_id,
        del:0
    };
    var encode_post = sign.encode_post();
    $.post(url_list.rewriting,encode_post,function (data) {
        if(type==1){
            getListPage(url_list.compose_list,ids[0],keyword,1,curr_ss[0]);
        }else {
            getListPage(url_list.myCompose_list,ids[0],keyword,2,curr_ss[0]);
        }

    })

}

//禁用
function status_compose(id,status){
    var id = id;
    var status = status;
    sign.params={
        id:id,
        status:status
    };
    var encode_post = sign.encode_post();
    $.post(url_list.rewriting,encode_post, function(data){
        //解码
        if(typeof data == 'string'){
            data = JSON.parse(data);
        }

        data = sign.decode_post(data);
        if(data.code==200){
            layer.msg(data.msg,{icon:1},1500);
            getListPage(url_list.compose_list,ids[0],keyword,1,curr_ss[0]);
        }else {
            layer.msg(data.msg,{icon:2},1500);
        }

    });
}
//批量禁用
function all_status_compose(status){
    var checkedNum = $("input[name='id[]']:checked").length;
    if (checkedNum == 0) {
        // alert("请选择至少一项！");
        layer.msg('请选择至少一项', {icon: 2});
        return;
    }
    // 批量选择
    var checkedList = new Array();
    $("input[name='id[]']:checked").each(function () {
        checkedList.push($(this).val());
    });
    var check_id =checkedList.join(',');
    var status = status;
    sign.params={
        id:check_id,
        status:status
    };
    var encode_post = sign.encode_post();
    $.post(url_list.rewriting,encode_post, function(data){
        if (typeof data== 'string'){
            data = JSON.parse(data);
        }
        data = sign.decode_post(data);
        if (data.code==200){
            layer.msg(data.msg,{icon:1},1500);
            getListPage(url_list.compose_list,ids[0],keyword,1,curr_ss[0]);
        }else {
            layer.msg(data.msg,{icon:2},1500);
        }

    });
}

//全选
$("#allChk").click(function() {
    $("input[name='id[]']").prop("checked",this.checked);
});

